<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width;initial-scale=1;maximum-scale=1;minimum-scale=1;user-scalable=no;">
    <title>Title</title>
    <style>
      *{
        margin: 0;padding: 0;
      }
        div {
            position: absolute;
            width: 300px;
            height: 300px;
            margin: auto;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            background-color: pink; /*方便看效果*/
        }
        .method1{
            /*确定容器的宽高宽500高300的层设置层的外边距div{*/
            position:absolute;/*绝对定位*/
            width:500px;
            height:300px;
            top:50%;
            left:50%;
            margin:-150px -250px;/*外边距为自身宽高的一半*/
            background-color:pink;/*方便看效果*/
        }
        .method2{
            /*未知容器的宽高，利用`transform`属性*/
            position: absolute;
            width: 300px;
            height: 500px;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
            background-color:pink;/*方便看 效果*/
        }
        /*利用flex布局实际使用时应考虑兼容性*/
        .method3{
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .method3 div{
            width: 100px;
            height: 100px;
            background-color: pink;
        }

        /*利用text-align:center和vertical-align:middle属性*/
        .container {
            position: fixed;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            text-align: center;
            font-size: 0;
            background: rgba(0, 0, 0, 0.5);
            white-space: nowrap;
            overflow: auto;
        }

        .container::after {
            content: "";
            display: inline-block;
            height: 100%;
            vertical-align: middle;
        }

        .box {
            display: inline-block;
            width: 500px;
            height: 400px;
            background-color: pink;
            white-space: normal;
            vertical-align: middle;
        }
    </style>
</head>
<body>

</body>
</html>